//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    titleBarHeight: '',
    tabs: [],
    activeTab: 0,
    list: [],
    lists: [],
    showLoading: true,
    searchData: '',
    pushOver: false,
    offset: 0,
    alpha: '',
    countryList: [],
    countryLists: [],
    countryIndex: 0,
    countryTabData: [],
    countryTabDatas: [],
    typeId: "",
    type: "",
    wenid: [],
    distilleryid: "", //酒柜传的id
    showView: false, //盒子弹框的显示隐藏
  },
  onLoad: function (options) {

    if (options.typeId == 'tt') {

      var that = this;
      console.log(options)
      that.setData({
        type: options.typeId,
        typeId: options.typeId
      })
      that.getData(0)

      that.setData({
        activeTab: 0
      })



      wx.getSystemInfo({
        success: function (res) {
          console.log(res, 'hahahahahhaha')
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })

    } else {
      var that = this;
      console.log(options)
      that.setData({
        typeId: options.typeId
      })
      that.getData(options.typeId)

      that.setData({
        activeTab: options.typeId
      })



      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            titleBarHeight: res.statusBarHeight + 46
          })
        }
      })

      const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
      const tabs = titles.map(item => ({
        title: item
      }))
      console.log(tabs)
      this.setData({
        tabs
      })
    }


  },
  onTabCLick(e) {
    const index = e.detail.index
    console.log(index)
    //切换栏目，重置数据
    this.setData({
      activeTab: index,
      showLoading: true,
      list: []
    })
    setTimeout((item) => {
      this.getData(index)
    }, 500)
  },
  scrollCountry(e) {
    console.log(e)
  },

  onChange(e) {
    // const index = e.detail.index
    // this.setData({activeTab: index})
  },
  navBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  getData(typeId) {
    console.log(typeId, 111)
    let that = this
    let countryLists = []
    if (that.data.pushOver) return
    let typeArr = ['whisky', 'brand', 'distillery', 'business']
    app.request({
      url: '/' + typeArr[typeId] + '/list-' + typeArr[typeId],
      success: function (res) {
        console.log(res)

        res.data.list.unshift(res.data.list.pop())
        console.log(res.data.list)


        if (typeId >= 1) {
          res.data.country.unshift('全部')
          countryLists = res.data.country
          res.data.list.map(item => {

            item.datas.map(itemb => {
              if (itemb.score < 1) itemb.score = '/'
            })
            return item
          })
        } else {
          countryLists = []
        }

        that.setData({
          list: that.data.list.concat(res.data.list),
          countryTabData: that.data.list.concat(res.data.list),
          countryList: countryLists
        })
        that.setData({
          showLoading: false
        })
      }
    })
  },
  searChitem() {
    this.setData({
      showView: !this.data.showView
    })
    console.log(this.data.showView)
  },
  goSearch(e) {
    console.log(e)
    let that = this
    let data = {
      typeId: that.data.activeTab,
      data: e.detail.value
    }
    console.log(data)
    wx.navigateTo({
      url: '/pages/search/index',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', data)
      }
    })
  },
  goDes(e) {
    //判断进入页面的路径
    if (this.data.type == 'tt') {
      let index = e.currentTarget.dataset.index;
      let parentindex = e.currentTarget.dataset.parentindex;
      this.data.wenid.push(e.currentTarget.dataset.item.wineid)
      this.data.list[parentindex].datas[index].active = true;
      this.setData({
        list: this.data.list,
        // ['list[' + index + '].datas[' + parentindex + '].active']: true,
        distilleryid: e.currentTarget.dataset.item.wineid,
        wenid: this.data.wenid
      })
      console.log(this.data.wenid)
    } else {
      let data = e.currentTarget.dataset.item
      console.log(data)
      data.typeId = this.data.activeTab
      if (data.typeId >= 1) {
        // wx.setStorageSync('data', data)
        console.log(JSON.stringify(data))
        wx.navigateTo({
          url: '/pages/details/index?id=' + JSON.stringify(data),
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            // res.eventChannel.emit('acceptDataFromOpenerPage', data)
          }
        })
      } else {
        console.log(e.currentTarget.dataset.item.wineid)

        wx.navigateTo({
          url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
        })
      }
    }



  },
  butt: function () {

    let yy = ''
    this.data.wenid.map(item => {
      if (yy == '') {
        yy += item
      } else {
        yy += `,${item}`
      }
    })
    console.log(yy)
    app.request({
      url: '/whisky/add-gradevin?uid=' + wx.getStorageSync('uid') + '&wineids=' + yy,
      success: function (res) {
        console.log(res)
        if (res.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
          })
          setTimeout(wx.navigateTo({
            url: '/pages/index/index',
          }), 2000)

        } else {
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
          })
        }
      }

    })
  },
  handlerAlphaTap(e) {
    let {
      ap
    } = e.target.dataset;
    this.setData({
      alpha: ap
    });
  },
  handlerMove(e) {
    let {
      list
    } = this.data;
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if (rY >= 0) {
      let index = Math.ceil((rY - this.apHeight) / this.apHeight);
      if (0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({
          alpha: nonwAp.alphabet
        });
      }
    }
  },
  filterData(e) {
    let name = e.currentTarget.dataset.country
    let index = e.currentTarget.dataset.index
    console.log(index)

    //切换后先还原数据
    this.data.list = this.data.countryTabData

    this.setData({
      countryIndex: index
    })
    if (name == '全部') {
      this.setData({
        list: this.data.list
      })
      return
    }

    var data = JSON.parse(JSON.stringify(this.data.list)); //深拷贝
    let datas = data.map(item => {
      let inData = item.datas.filter(itemb => {
        if (itemb.country == name) {
          return itemb
        }
      })
      item.datas = inData
      return item
    })
    console.log(datas)
    this.setData({
      list: datas
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底')
    // this.getData(this.data.searchData, this.data.activeTab)
  },

})